package com.infoland.afanmall.model.my.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.dialog.OpenTuiKuanDialog;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.my.entity.OrderStatusEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 王永吉
 * @date 2018/7/18 14:43
 * 类型 全部-0，待付款-1，待发货-2，待收货-3，完成-4，有退款-5 待评价-6
 */
public class OrderStatusActivity extends AppCompatActivity {
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refrensh)
    SmartRefreshLayout refrensh;
    ArrayList<OrderStatusEntity> arrayList;
    BaseQuickAdapter<OrderStatusEntity, BaseViewHolder> baseQuickAdapter;
    BaseQuickAdapter<OrderStatusEntity, BaseViewHolder> twoQuickAdapter;
    private int orderStatus = 0;
    private boolean isLastPage = false;
    private int pageNumber = 1;
    private int pageSize = 6;

    private int getLayout() {
        return R.layout.order_status_activity;
    }

    private void initData() {
        arrayList = new ArrayList<>();
        arrayList.add(new OrderStatusEntity());
        arrayList.add(new OrderStatusEntity());
        arrayList.add(new OrderStatusEntity());
        recycler.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        getOrder();

    }

    //todo 初始化适配器
    private void InitAdapter() {
        baseQuickAdapter = new BaseQuickAdapter<OrderStatusEntity, BaseViewHolder>(R.layout.tui_kuan_item, arrayList) {
            @Override
            protected void convert(BaseViewHolder helper, OrderStatusEntity item) {
                helper.getView(R.id.by_again).setOnClickListener(v -> {
                    OpenTuiKuanDialog openTuiKuanDialog = new OpenTuiKuanDialog(OrderStatusActivity.this);
                    NiceSpinner niceSpinner = openTuiKuanDialog.getNiceSpinner();
                    List<String> dataset = new LinkedList<>(Arrays.asList("One", "Two", "Three", "Four", "Five"));
                    niceSpinner.attachDataSource(dataset);
                    openTuiKuanDialog.getTui_kuan_bt1().setOnClickListener(v1 -> {

                    });
                    openTuiKuanDialog.getTui_kuan_bt2().setOnClickListener(v2 -> {

                    });
                    openTuiKuanDialog.show();
                    ToastUtils.showShort(helper.getAdapterPosition() + "");
                });
                TwoAdapter(helper);

//                if (baseQuickAdapter.getData().size() - 1 == helper.getAdapterPosition()) {
//                    ToolsKit.addMarginDp(helper.getView(R.id.tui_kuan_layout), 0, 0, 0, 50);
//                }
            }
        };
        recycler.setAdapter(baseQuickAdapter);
    }

    //todo 创建二层适配器
    private void TwoAdapter(BaseViewHolder helper) {
        RecyclerView orderMyItemRecy = helper.getView(R.id.tui_kuan_recycler);
        orderMyItemRecy.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        twoQuickAdapter = new BaseQuickAdapter<OrderStatusEntity, BaseViewHolder>(R.layout.order_my_item_item, arrayList) {
            @Override
            protected void convert(BaseViewHolder helper, OrderStatusEntity item) {
            }
        };
        orderMyItemRecy.setAdapter(twoQuickAdapter);
    }

    //todo 开始查询订单
    private void getOrder() {

        if (isLastPage) {
            ToastUtils.showShort("已经是最后一页");
        } else {
            new OkHttpKit().send(
                    response200 -> {
                        //第一层适配器
                        InitAdapter();
                    },
                    success401 -> {
                        InitAdapter();
                    },
                    error -> {
                        InitAdapter();
                    }
            ).post(CONST.ORDER_LIST + UserKit.getAccessToken(), getPagePara(), this);
        }
    }

    //todo    获取分页参数
    private JSONObject getPagePara() {
        JSONObject para = new JSONObject();
        para.put("pageNumber", pageNumber); //页数
        para.put("pageSize", pageSize); //条数
        para.put("type", orderStatus);
        return para;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        StatusBarKit.immersiveBlackTitleText(this, titleLayout);
        refrensh.setEnableRefresh(false);
        if (getIntent().getExtras() != null) {
            orderStatus = getIntent().getExtras().getInt("orderStatus");
        }
        initData();
    }

    @Override
    public void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }

}
